package com.tencent.sr.iris.activity.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.tencent.sr.rmall.springbootstarter.utils.CRC32Util;

/**
 * 描述用途
 *
 * @author ylongzhang
 * @date 2022/07/18
 */
public class ShardingIdUtil {

    public static final int SHARDING_NUM = 1024;

    public static int doSharding(String uid) {
        return (int) (CRC32Util.crc32(uid) % SHARDING_NUM);
    }

    public static int sharding(Object shardingKey) {
        if (ObjectUtil.isNull(shardingKey)) {
            return 0;
        }
        return (int) (CRC32Util.crc32(String.valueOf(shardingKey)) % SHARDING_NUM);
    }

    public static void main(String[] args) {
        outletShardingStrategy("180000213089792");
    }

    /**
     * 100000045509699    15    486
     * 100000045507066    25    825
     * 100000045509445    31    1006
     *
     * @param shardingId
     */
    private static void outletShardingStrategy(String shardingId) {
        int dataBaseIndex = doSharding(shardingId) / 32;
        long tableIndex = doSharding(shardingId);
        System.out.println(shardingId + "    " + dataBaseIndex + "    " + tableIndex);
    }
}
